package com.sy.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sy.blog.dao.mapper.CommentMapper;
import com.sy.blog.dao.pojo.Comment;
import com.sy.blog.dao.pojo.SysUser;
import com.sy.blog.service.CommentService;
import com.sy.blog.service.SysUserService;
import com.sy.blog.utils.UserThreadLocal;
import com.sy.blog.vo.CommentVo;
import com.sy.blog.vo.Result;
import com.sy.blog.vo.UserVo;
import com.sy.blog.vo.params.CommentParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SysUserService sysUserService;


    /**
     * 1、根据文章id查出评论内容
     * 2、根据作者id查出作者信息
     * 3、查询所有level为1的评论，并查出他们的子评论（parent_id）
     * 4、查出是对谁评论的
     * @return
     */
    @Override
    public Result findCommentByArticleId(Long id) {

        LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentLambdaQueryWrapper.eq(Comment::getArticleId, id);
        // 先查出所有level为1的，即全部的父评论
        commentLambdaQueryWrapper.eq(Comment::getLevel, 1);
        List<Comment> comments = commentMapper.selectList(commentLambdaQueryWrapper);
        return Result.success(copyList(comments));
    }

    /**
     * 1、请求被登录拦截，所以先从ThreadLocal中获取登录的User对象
     * 2、赋值给评论并插入数据库后显示
     * @param commentParams
     * @return
     */
    @Override
    public Result doComment(CommentParams commentParams) {
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setArticleId(commentParams.getArticleId());
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParams.getContent());
        comment.setCreateDate(System.currentTimeMillis());
        Long parent = commentParams.getParent();
        if (parent == null || parent == 0) {
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        comment.setParentId(parent == null ? 0 : parent);
        Long toUserId = commentParams.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        this.commentMapper.insert(comment);
        return Result.success(null);
    }

    private List<CommentVo> copyList(List<Comment> comments) {
        ArrayList<CommentVo> commentVos = new ArrayList<>();
        for (Comment comment : comments) {
            commentVos.add(copy(comment));
        }
        return commentVos;
    }


    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment, commentVo);
        commentVo.setId(String.valueOf(comment.getId()));
        // 查出作者的信息并赋值
        Long authorId = comment.getAuthorId();
        UserVo userVo = sysUserService.findUserVoByAuthorId(authorId);
        commentVo.setAuthor(userVo);
        // 获取子评论
        List<CommentVo> commentChild = findChildCommentByParentId(comment.getId());
        commentVo.setChildrens(commentChild);
        // 获取to_use
        if (comment.getLevel() > 1) {  // 2级才有可能父标题
            Long toUid = comment.getToUid();
            UserVo toUserVo = sysUserService.findUserVoByAuthorId(toUid);
            commentVo.setToUser(toUserVo);
        }
        return commentVo;
    }

    /**
     * 根据父评论的id获取子评论
     * 子评论满足的要求：1、父评论id   2、level为 2
     * @param id
     * @return
     */
    private List<CommentVo> findChildCommentByParentId(Long id) {
        LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentLambdaQueryWrapper.eq(Comment::getParentId, id);
        commentLambdaQueryWrapper.eq(Comment::getLevel, 2);
        List<Comment> comments = commentMapper.selectList(commentLambdaQueryWrapper);
        return copyList(comments);
    }
}
